<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>

        <el-tabs>
          <!-- 左侧 -->
          <el-tab-pane label="人员权限">

            <el-table row-key="id" :data="list" border>
              <el-table-column label="序号" prop="name">
                <template #default="{ row }">
                  {{ row.id }}
                </template>
              </el-table-column>
              <!-- <el-table-column label="用户名" prop="username" />
              <el-table-column label="密码" prop="password" /> -->
              <el-table-column label="仓储权限" prop="statusDepository" />
              <el-table-column label="订单权限" prop="statusOrder" />
              <el-table-column label="财务权限" prop="statusAccount" />
              <el-table-column label="报价权限" prop="statusPrice" />
              <el-table-column label="客户管理权限" prop="statusUser" />
              <el-table-column label="营销系统权限" prop="statusSell" />
              <el-table-column label="权限管理" prop="statusPermission" />
              <el-table-column label="系统自定义权限" prop="statusCustom" width="120" />
              <el-table-column label="操作" fixed="right" >
                <template slot-scope="scope">
                  <el-button size="small" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
                </template>

              </el-table-column>
            </el-table>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 弹框 -->
      <el-dialog :title="showTitle" :visible="showDialog" @close="closeDialog">
        <el-form v-loading="loading" label-width="110px" >
          <el-form-item label="序号" prop="id" >
            <el-input v-model="formData.id" placeholder="请输入"  />
          </el-form-item>
          <!-- <el-form-item label="用户名" prop="username">
            <el-input v-model="formData.username" placeholder="请输入" disabled />
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="formData.password" placeholder="请输入" disabled />
          </el-form-item> -->
          <el-form-item label="仓储权限" prop="statusDepository">
            <el-input v-model="formData.statusDepository" placeholder="请输入"  />
          </el-form-item>
          <el-form-item label="订单权限" prop="statusOrder">
            <el-input v-model="formData.statusOrder" placeholder="请输入"  />
          </el-form-item>
          <el-form-item label="财务权限" prop="statusAccount">
            <el-input v-model="formData.statusAccount" placeholder="请输入"  />
          </el-form-item>
          <el-form-item label="报价权限" prop="statusPrice">
            <el-input v-model="formData.statusPrice" placeholder="请输入"  />
          </el-form-item>
          <el-form-item label="客户管理权限" prop="statusUser" >
            <el-input v-model="formData.statusUser" placeholder="请输入"  />
          </el-form-item>
          <el-form-item label="营销管理权限" prop="statusSell">
            <el-input v-model="formData.statusSell" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="权限管理" prop="statusPermission">
            <el-input v-model="formData.statusPermission" placeholder="请输入"  />
          </el-form-item>
          <el-form-item label="系统自定义权限" prop="statusCustom">
            <el-input v-model="formData.statusCustom" placeholder="请输入" />
          </el-form-item>
          <!-- <el-form-item label="角色" prop="password">
           <el-select v-model="formData.password" filterable placeholder="请选择">
            <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
         </el-option>
       </el-select>
        </el-form-item> -->
          
        </el-form>

        <template #footer>
          <div style="text-align: right;">
            <el-button @click="closeDialog">取消</el-button>
            <el-button type="primary" @click="clickSubmit">确认</el-button>
          </div>
        </template>
      </el-dialog>

    </div>

    <!-- 分页 -->
    <div align="center">
      <el-pagination
        :current-page.sync="currentPage"
        layout="prev, pager, next, jumper"
        :total="list.length"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import {selectBUser,updateBUser} from '@/api/role'
export default {
  name: 'Permission',
  data() {
    return {
      dialogVisible: false,
      // 分页数据
      currentPage: 1,
      list: [],
      showDialog: false,
      formData: {
        // enVisible: '0', // 0不开启, 1开启 (注意: 不是布尔值, 所以将来需要处理switch)
        username: '', // 名称
        password:''
      },
      loading: false,
      permissionData: [] // 存放用于展示的权限列表数据 (树形结构)
    }
  },
  computed: {
    showTitle() {
      if (this.formData.id) {
        return '编辑'
      } 
    }
  },
  mounted() {
    console.log('进入页面的钩子函数')
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {

      const data = await selectBUser()
      this.list = data
    },
    clickAdd(type, pid) {
      this.showDialog = true
      // 点击显示弹框时, 立刻记录type, pid
      // this.formData.type = type
      // this.formData.pid = pid
    },
    closeDialog() {
      this.showDialog = false
      // 重置清空数据
    
    },
    // 点击弹框的确认按钮
    async clickSubmit() {
     
      if (this.formData.id) {
        // 编辑操作
        const {postalcode,...formData} = this.formData
        await updateBUser(formData)
      } 
      this.showDialog = false
      this.$message.success('操作成功')
      this.getPermissionList()
    },
    handleEdit(row){
      console.log(row)
      // const {postalCode,...formData} = row
      // formData.postalcode = postalCode
      this.formData = row;
      this.formData.postalcode = row.postalCode
      this.showDialog = true
    },
    // 点击查看编辑按钮, 显示弹框
    clickEdit(id) {
      // 显示弹框
      this.showDialog = true
      this.loading = true
      // 发送请求, 获取数据, 进行回显
      // const { data } = await reqGetPermissionDetail(id)
      // this.formData = data

      this.loading = false
    },

    // 分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
    }
  }
}
</script>

<style>

</style>
